package cn.tedu._05interceptor.base.config;

import cn.tedu._05interceptor.base.interceptors.MyInterceptor1;
import cn.tedu._05interceptor.base.interceptors.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    //addI + 回车
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //策略1: 拦截所有请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/**");

        //策略2: 拦截指定的一个或者多个请求,比如:拦截注册和登录2个请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/user/reg", "/v1/user/login");

        //策略3: 拦截指定的多个请求,比如:拦截用户模块的所有请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/user/**");

        //策略4: 排除指定的请求,比如:拦截用户模块中除了登录之外的所有请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/user/**")
        //        .excludePathPatterns("/v1/user/login");

        //演示多个拦截器的执行顺序
        /*
            List集合1: [pre1,   pre2]   从头到位依次获取并执行方法;
            List集合2: [post1,  post2]  从尾到头依次获取并执行方法;
            List集合3: [after1, after2] 从尾到头依次获取并执行方法;
         */
        registry.addInterceptor(new MyInterceptor1())
                .addPathPatterns("/v1/**");
        registry.addInterceptor(new MyInterceptor2())
                .addPathPatterns("/v1/**");
    }
}




